#include "view_times.h"
#include "view_time_0.h"
#include "view_time_1.h"
#include "view_time_2.h"
#include "view_time_3.h"
#include "view_time_4.h"
#include "view_time_5.h"
#include "view_time_6.h"
#include "view_time_7.h"
#include "view_time_8.h"
#include "view_time_9.h"
#include "view_time_10.h"
#include "view_time_11.h"
#include "view_time_12.h"
#include "view_time_13.h"
#include "view_time_14.h"
#include "view_time_15.h"
#include "view_time_16.h"
#include "view_time_17.h"
#include "view_time_18.h"
#include "view_time_19.h"
#include "view_time_20.h"
#include "view_time_21.h"
#include "view_time_22.h"
#include "view_time_23.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "font_mngr.h"
#include "view_common.h"

#include "sports_mode.h"
#include "m_product_test.h"
#include "system/view_system.h"
#include "weather_ctrl.h"
#include "math.h"
#include "stopwatch.h"
#ifdef USE_CT1711
#include "body_temperature.h"
#include "ct1711.h"
#endif
//#include "style_911_message.h"
#include "app_trace.h"
#define LOG(fmt, ...) // app_trace_log(fmt "\r\n", ##__VA_ARGS__)
void view_time_base_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
#if 0
#warning debug
        disp_ctrl.com_data.i_temp[30] = HOME_PAGE_TYPE_5;
#else
        disp_ctrl.com_data.i_temp[30] = home_page_type_get();
#endif
        if (disp_ctrl.com_data.i_temp[30] != HOME_PAGE_TYPE_6)
        {
            disp_ctrl.auto_refresh_time = 500;
            datetime_t UIdatetime = DateSync();
            disp_ctrl.com_data.i_temp[1] = UIdatetime.date.month;
            disp_ctrl.com_data.i_temp[2] = UIdatetime.date.day;
            disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
            disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
            disp_ctrl.com_data.i_temp[5] = get_present_week();
            disp_ctrl.com_data.i_temp[6] = ble_if_connected();
            disp_ctrl.com_data.i_temp[7] = battery_ctrl_get_percent();
            disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
            disp_ctrl.com_data.i_temp[9] = if_time_hour_system_12();
            if (disp_ctrl.com_data.i_temp[9])
            {
                disp_ctrl.com_data.i_temp[9] += (disp_ctrl.com_data.i_temp[3] >= 12); // 1=am,2=pm
                disp_ctrl.com_data.i_temp[3] %= 12;
                if (disp_ctrl.com_data.i_temp[3] == 0)
                {
                    disp_ctrl.com_data.i_temp[3] = 12;
                }
            }
            disp_ctrl.com_data.i_temp[11] = sport_get_present_step();
            disp_ctrl.com_data.i_temp[16] = sport_get_calorie();
            disp_ctrl.com_data.i_temp[17] = hr_get_hr_data();
            disp_ctrl.com_data.i_temp[10] = (disp_ctrl.com_data.i_temp[11] * 100) / system_config_get_target_step(); //完成度百分比0~100;暂时默认目标5000步
            if (disp_ctrl.com_data.i_temp[10] > 100)
            {
                disp_ctrl.com_data.i_temp[10] = 100;
            }
            disp_ctrl.com_data.i_temp[10] = (disp_ctrl.com_data.i_temp[10] * 36) / 10; //完成比例转换成角度
            weather_record_t weather = {0};
            weather_ctrl_get(0, &weather); //获取天气
            if (weather.date_index == 0)
            {
                disp_ctrl.com_data.i_temp[22] = 0;
            }
            else
            {
                disp_ctrl.com_data.i_temp[22] = 1;
            }
            disp_ctrl.com_data.i_temp[21] = weather_get_temperature_type();
            //如果是华氏度需要转换
            if (disp_ctrl.com_data.i_temp[21] != WEATHER_TEMPERATURE_TYPE_C)
            {
                weather.temperature_min = round(32 + weather.temperature_min * 1.8);
                weather.temperature_max = round(32 + weather.temperature_max * 1.8);
            }
            disp_ctrl.com_data.i_temp[18] = weather.type;
            disp_ctrl.com_data.i_temp[19] = weather.temperature_min;
            disp_ctrl.com_data.i_temp[20] = weather.temperature_max;
#ifdef USE_CT1711
            disp_ctrl.com_data.f_temp[25] = get_wrist_temperature();
            disp_ctrl.com_data.f_temp[26] = get_body_temperature();
#endif
// #warning debug
//             disp_ctrl.com_data.i_temp[22] = 1;
//             disp_ctrl.com_data.i_temp[21] = WEATHER_TEMPERATURE_TYPE_C;
//             disp_ctrl.com_data.i_temp[18] = WEATHER_TYPE_SUNSHINE;
//             disp_ctrl.com_data.i_temp[19] = 20;
//             disp_ctrl.com_data.i_temp[20] = 30;
//             disp_ctrl.com_data.i_temp[17] = 107;
//             disp_ctrl.com_data.f_temp[26] = 33.13f;
        }
    }

    switch (disp_ctrl.com_data.i_temp[30])
    {
#if !defined(TIME_0_SKIP)
    case HOME_PAGE_TYPE_0:
        view_time_0_display();
        break;
#endif
#if !defined(TIME_1_SKIP)
    case HOME_PAGE_TYPE_1:
        view_time_1_display();
        break;
#endif
#if !defined(TIME_2_SKIP)
    case HOME_PAGE_TYPE_2:
        view_time_2_display();
        break;
#endif
#if !defined(TIME_3_SKIP)
    case HOME_PAGE_TYPE_3:
        view_time_3_display();
        break;
#endif
#if !defined(TIME_4_SKIP)
    case HOME_PAGE_TYPE_4:
        view_time_4_display();
        break;
#endif
#if !defined(TIME_5_SKIP)
    case HOME_PAGE_TYPE_5:
        view_time_5_display();
        break;
#endif
#if !defined(TIME_6_SKIP)
    case HOME_PAGE_TYPE_6:
        view_time_6_display();
        break;
#endif
#if !defined(TIME_7_SKIP)
    case HOME_PAGE_TYPE_7:
        view_time_7_display();
        break;
#endif
#if !defined(TIME_8_SKIP)
    case HOME_PAGE_TYPE_8:
        view_time_8_display();
        break;
#endif
#if !defined(TIME_9_SKIP)
    case HOME_PAGE_TYPE_9:
        view_time_9_display();
        break;
#endif
#if !defined(TIME_10_SKIP)
    case HOME_PAGE_TYPE_10:
        view_time_10_display();
        break;
#endif
#if !defined(TIME_11_SKIP)
    case HOME_PAGE_TYPE_11:
        view_time_11_display();
        break;
#endif
#if !defined(TIME_12_SKIP)
    case HOME_PAGE_TYPE_12:
        view_time_12_display();
        break;
#endif
#if !defined(TIME_13_SKIP)
    case HOME_PAGE_TYPE_13:
        view_time_13_display();
        break;
#endif
#if !defined(TIME_14_SKIP)
    case HOME_PAGE_TYPE_14:
        view_time_14_display();
        break;
#endif
#if !defined(TIME_15_SKIP)
    case HOME_PAGE_TYPE_15:
        view_time_15_display();
        break;
#endif
#if !defined(TIME_16_SKIP)
    case HOME_PAGE_TYPE_16:
        view_time_16_display();
        break;
#endif
#if !defined(TIME_17_SKIP)
    case HOME_PAGE_TYPE_17:
        view_time_17_display();
        break;
#endif
#if !defined(TIME_18_SKIP)
    case HOME_PAGE_TYPE_18:
        view_time_18_display();
        break;
#endif
#if !defined(TIME_19_SKIP)
    case HOME_PAGE_TYPE_19:
        view_time_19_display();
        break;
#endif
#if !defined(TIME_20_SKIP)
    case HOME_PAGE_TYPE_20:
        view_time_20_display();
        break;
#endif
#if !defined(TIME_21_SKIP)
    case HOME_PAGE_TYPE_21:
        view_time_21_display();
        break;
#endif
#if !defined(TIME_22_SKIP)
    case HOME_PAGE_TYPE_22:
        view_time_22_display();
        break;
#endif
#if !defined(TIME_23_SKIP)
    case HOME_PAGE_TYPE_23:
        view_time_23_display();
        break;
#endif
    default:
#if defined(DEFAULT_TIME)
        home_page_type_set(DEFAULT_HOME_PAGE);
        DEFAULT_TIME();
#else
#warning debug
        view_time_0_display();
#endif
        break;
    }
}
view_event_result_t view_time_event(int view, event_param_t const *param)
{
    switch (param->event)
    {
    case event_view_entry:
        // LOG("view_time_event_view_entry %d", hr_if_band_wear());
#if !defined(TIME_6_SKIP)
        stopwatch_start();
        disp_ctrl.com_data.i_temp[41] = -1;
        disp_ctrl.com_data.i_temp[42] = 1;
        disp_ctrl.com_data.i_temp[43] = 1;
#endif
#ifdef USE_CT1711
        ct1711_start();
#endif
        return view_event_result_handled;
    case event_view_re_screen_on:
#if !defined(TIME_6_SKIP)
        disp_ctrl.com_data.i_temp[41] = -1;
        disp_ctrl.com_data.i_temp[42] = 1;
        disp_ctrl.com_data.i_temp[43] = 1;
#endif
        return view_event_result_handled;
    case event_view_exit:
#if !defined(TIME_6_SKIP)
        stopwatch_stop();
#endif
#ifdef USE_CT1711
        ct1711_stop();
#endif
        return view_event_result_handled;
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP_LONG:
#endif
        if (product_test_state_get() == PRODUCT_TEST_STATE_TEST_ASSEM)
        {
            home_page_type_switch();
#if !defined(TIME_6_SKIP)
            disp_ctrl.com_data.i_temp[41] = -1;
            disp_ctrl.com_data.i_temp[42] = 1;
            disp_ctrl.com_data.i_temp[43] = 1;
#endif
            display_show(view_time, DISP_MODE_DOWN_MOVE);
        }
        return view_event_result_handled;

#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
        return view_event_result_handled;
#endif
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
        return view_event_result_handled;
#endif
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
    {
        sports_mode_state_t state = (sports_mode_state_t)sports_mode_get_state();
        switch (state)
        {
            //        case sports_mode_state_pause:
            //            display_show(view_sports_paused, DISP_MODE_DOWN_MOVE);
            //            return view_event_result_handled;
        case sports_mode_state_normal:
            display_show(view_sports_state_0, DISP_MODE_DOWN_MOVE);
            return view_event_result_handled;
        }
    }
    }
    return view_event_result_not_handled;
}
void view_time_display(int view)
{
    switch (view)
    {
    case view_time_base:
        view_time_base_display();
        break;
#ifdef STYLE_911
    case view_time_message:
        style_911_time_message_display();
        break;
#endif
    }
}
